// src/api/auth.ts
import axios from 'axios';
import type { LoginData, RegisterData, AuthResponse, User } from '@/types';

const api = axios.create({
  baseURL: import.meta.env.VITE_API_BASE_URL,
  timeout: 10000
});

export const authApi = {
  async login(credentials: LoginData): Promise<AuthResponse> {
    const response = await api.post<AuthResponse>('/auth/login', credentials);
    return response.data;
  },

  async register(userData: RegisterData): Promise<AuthResponse> {
    const response = await api.post<AuthResponse>('/auth/register', userData);
    return response.data;
  },

  async logout(token: string): Promise<void> {
    await api.post('/auth/logout', null, {
      headers: { Authorization: `Bearer ${token}` }
    });
  },

  async refreshToken(token: string): Promise<AuthResponse> {
    const response = await api.post<AuthResponse>('/auth/refresh', null, {
      headers: { Authorization: `Bearer ${token}` }
    });
    return response.data;
  },

  async getUserProfile(token: string): Promise<User> {
    const response = await api.get<User>('/auth/me', {
      headers: { Authorization: `Bearer ${token}` }
    });
    return response.data;
  }
};